package com.lobaby.tools.base.controller;

import com.lobaby.tools.common.utils.StringUtil;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.util.Enumeration;
import java.util.HashMap;

/**
 * @author Administrator
 *         Created by Administrator on 2019/11/26 0026.
 */
@Controller
public class BaseController {
    @RequestMapping(value = "/views/{foldName}/{fileName}" , method = {RequestMethod.GET,RequestMethod.POST })
    public String pages(HttpSession session, @PathVariable String foldName, @PathVariable(required = false) String fileName) {
        if (StringUtil.isBlank(fileName)) {
            return foldName;
        } else {
            return foldName+"/"+fileName;
        }
    }

    @RequestMapping("/")
    public String init() {
        return "main";
    }

    @SuppressWarnings("rawtypes")
    public static HashMap<String, String> getRequestParam(HttpServletRequest request) throws Exception {
        Enumeration eObj = request.getParameterNames();
        HashMap<String, String> returnMap = null;

        if (eObj != null) {
            returnMap = new HashMap<String, String>();

            while (eObj.hasMoreElements()) {
                Object obj = eObj.nextElement();
                if (obj == null || obj.toString().trim().length() < 1
                        || obj.toString().trim().equals("m") || obj.toString().equals(request.getQueryString())){
                    continue;
                }

                Object val = null;
                val = request.getParameter(obj.toString());

                if (val == null || (val instanceof String && val.toString().trim().length() == 0)) {
                    continue;
                }
                returnMap.put(obj.toString(), val == null ? null : val.toString());
            }

        }

        return returnMap;
    }
}
